package demo1;

import java.util.HashMap;

/**d
 * @Author liangzai
 * @Description:
 */
public class Test {
    public static void main(String[] args) {
        String str = "liangasd";
        char[] ch = str.toCharArray();
        str = String.copyValueOf(ch,1,str.length()-1);
        for (int i = 0; i < str.length(); i++) {
            char ch1 = str.charAt(i);
            System.out.print(ch1);
        }
    }

    public int searchInsert(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]>=target){
                return i;
            }
        }
        return nums.length;
    }

    //水果成篮
    public int totalFruit1(int[] fruits) {
        HashMap<Integer,Integer> map = new HashMap<>();
        int len = 0;
        for(int left=0,right = 0;right<fruits.length;right++){
            if(!map.containsKey(fruits[right])){
                map.put(fruits[right],1);
            }else {
                int val = map.get(fruits[right]);
                map.put(fruits[right],val+1);
            }
            while (map.size() > 2) {
                int val = map.get(fruits[left]);
                map.put(fruits[left],val-1);
                if (map.get(fruits[left]) == 0) {
                    map.remove(fruits[left]);
                }
                left++;
            }
            len = Math.max(len,right-left+1);
        }
        return len;
    }

    public int totalFruit(int[] fruits) {
        int l = fruits.length;
        int ans = 0;
        int m = 0;
        int[] temp = new int[l];
        for(int n = 0,ttl = 0;n<l;n++){
            if(++temp[fruits[n]] == 1) ttl++;
            while(ttl > 2){
                ans = Math.max(ans,n - m);
                if(--temp[fruits[m++]] == 0) ttl--;
            }
        }
        return Math.max(ans,l - m);

    }

    //将x减到0的最小操作数
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        int target = sum-x;
        if (target < 0) {
            return -1;
        }
        int left = 0;
        int right = 0;
        int len = -1;
        int val = 0;
        while (right < nums.length) {
            val += nums[right];
            while (val > target) {
                val -= nums[left++];
            }
            if (val == target) {
                len = Math.max(len,right-left+1);
            }
            right++;
        }
        return len==-1?len:nums.length-len;
    }
}
